package main.java;

import org.json.JSONException;
import org.json.JSONObject;

public class Auction
  implements Comparable<Auction>
{
  public int AuctionTime;
  public int BIN;
  public String BidState;
  public CardInstance Card;
  public int CurrentBid;
  public long ID;
  public int Offers;
  public int SellerID;
  public String SellerName;
  public int StartingBid;
  public String TradeState;
  private int expiration;
  private long expirationSecond;

  public Auction()
  {
  }

  public Auction(JSONObject paramJSONObject)
    throws JSONException
  {
    this.ID = paramJSONObject.getLong("tradeId");
    this.StartingBid = paramJSONObject.getInt("startingBid");
    this.CurrentBid = paramJSONObject.getInt("currentBid");
    this.BIN = paramJSONObject.getInt("buyNowPrice");
    this.Offers = paramJSONObject.optInt("offers");
    this.TradeState = paramJSONObject.getString("tradeState");
    this.expiration = paramJSONObject.optInt("expires", -1000);
    if (this.expiration >= 0)
      setExpiration(this.expiration);
    if (this.TradeState == null)
      this.TradeState = "free";
    this.BidState = paramJSONObject.optString("bidState", null);
    this.Card = new CardInstance(paramJSONObject.getJSONObject("itemData"));
  }

  private int getState()
  {
    int i;
    if (isClosed())
      i = 0;
    while (true)
    {
      return i;
      if (isActive())
        i = 2;
      else
        i = 1;
    }
  }

  public int compareTo(Auction paramAuction)
  {
    int i = getState() - paramAuction.getState();
    int j;
    if (i != 0)
      j = i;
    while (true)
    {
      return j;
      if (isActive())
      {
        int k = (int)(this.expirationSecond - paramAuction.expirationSecond);
        if (k != 0)
          j = k;
        else
          j = (int)(this.ID - paramAuction.ID);
      }
      else if (this.Card.Id > paramAuction.Card.Id)
      {
        j = 1;
      }
      else if (this.Card.Id < paramAuction.Card.Id)
      {
        j = -1;
      }
      else
      {
        j = 0;
      }
    }
  }

  public String getExpiration()
  {
    StringBuffer localStringBuffer;
    if (isActive())
    {
      int i = getSecondsRemaining();
      if (i > 0)
      {
        localStringBuffer = new StringBuffer();
        int j = i / 86400;
        if (j > 0)
        {
          localStringBuffer.append(j).append(" day");
          if (j > 1)
            localStringBuffer.append('s');
        }
        int k = i / 3600 - j * 24;
        if (k > 0)
        {
          localStringBuffer.append(' ').append(k).append(" hour");
          if (k > 1)
            localStringBuffer.append('s');
        }
        if (j == 0)
        {
          int m = i / 60 - k * 60;
          if (m > 0)
            localStringBuffer.append(' ').append(m).append(" min");
          if (k == 0)
          {
            int n = i - m * 60;
            localStringBuffer.append(' ').append(n).append(" sec");
          }
        }
      }
    }
    for (String str = localStringBuffer.toString(); ; str = null)
      return str;
  }

  public int getSecondsRemaining()
  {
    return (int)(this.expirationSecond - System.currentTimeMillis() / 1000L);
  }

  public boolean hasBid()
  {
    if (((this.CurrentBid > 0) || (this.Offers > 0)) && (!isClosed()));
    for (boolean bool = true; ; bool = false)
      return bool;
  }

  public boolean isActive()
  {
    return "active".equals(this.TradeState);
  }

  public boolean isClosed()
  {
    return "closed".equals(this.TradeState);
  }

  public boolean isFree()
  {
    if ((!isActive()) && (!isClosed()));
    for (boolean bool = true; ; bool = false)
      return bool;
  }

  public boolean isHighestBidder()
  {
    if ((!"highest".equals(this.BidState)) && (!"buyNow".equals("bidState")));
    for (boolean bool = false; ; bool = true)
      return bool;
  }

  public boolean isRemoving()
  {
    return "removing".equals(this.TradeState);
  }

  public boolean isWon()
  {
    if ((isClosed()) && (isHighestBidder()));
    for (boolean bool = true; ; bool = false)
      return bool;
  }

  public void setExpiration(int paramInt)
  {
    this.expirationSecond = (System.currentTimeMillis() / 1000L + paramInt);
  }
}
